#!/bin/bash
# =============================================================
# 脚本名称：set_local_yum.sh
# 适用系统：RedHat/CentOS/Rocky/AlmaLinux 6/7/8/9（及兼容发行版）
# 功能说明：
#   - 一键自动配置本地或网络yum源，支持阿里云和EPEL源
#   - 检测网络连通性，自动切换可用菜单
#   - 支持RHEL 8/9 AppStream/BaseOS分目录本地源
#   - 自动备份原有repo文件，防止误操作
#   - 交互式菜单，支持如下操作：
#       1) 配置网络yum源（阿里云，含EPEL）
#       2) 配置本地yum源（支持自定义路径，默认/mnt）
#       3) 单独配置EPEL源（适合已配置主源但需扩展第三方包场景）
# 使用方法：
#   bash set_local_yum.sh
#   # 按提示选择菜单并输入路径，无需命令行参数
#   # 兼容老用法：bash set_local_yum.sh /your/local/repo/path
# =============================================================

# 检测网络连通性
PING_TARGET="baidu.com"
echo "正在测试网络连通性(ping $PING_TARGET)..."
if ping -c 2 -W 2 $PING_TARGET >/dev/null 2>&1; then
    echo "网络联通正常。"
    echo "============================="
    echo "  1) 配置网络yum源（阿里云）"
    echo "  2) 配置本地yum源"
    echo "  3) 配置EPEL源"
    echo "============================="
    read -p "请输入数字 [1/2/3]，默认1: " menu
    menu=${menu:-1}
else
    echo "网络不可用，仅可配置本地yum源。"
    menu=2
fi

if [ "$menu" = "1" ]; then
    # 配置阿里云yum源+epel
    OSVER=0
    if [ -f /etc/redhat-release ]; then
        OSVER=$(grep -oE '[0-9]+' /etc/redhat-release | head -1)
    fi
    REPO_FILE="/etc/yum.repos.d/aliyun.repo"
    echo "生成阿里云yum源配置: $REPO_FILE"
    case $OSVER in
        6)
            cat > $REPO_FILE <<EOF
[aliyun]
name=Aliyun YUM Repo
baseurl=https://mirrors.aliyun.com/centos/6/os/x86_64/
gpgcheck=0
enabled=1
EOF
            cat > /etc/yum.repos.d/epel.repo <<EOF
[epel]
name=Extra Packages for Enterprise Linux 6 - $basearch
baseurl=https://mirrors.aliyun.com/epel/6/$basearch/
gpgcheck=0
enabled=1
EOF
            ;;
        7)
            cat > $REPO_FILE <<EOF
[aliyun]
name=Aliyun YUM Repo
baseurl=https://mirrors.aliyun.com/centos/7/os/x86_64/
gpgcheck=0
enabled=1
EOF
            cat > /etc/yum.repos.d/epel.repo <<EOF
[epel]
name=Extra Packages for Enterprise Linux 7 - $basearch
baseurl=https://mirrors.aliyun.com/epel/7/$basearch/
gpgcheck=0
enabled=1
EOF
            ;;
        8)
            cat > $REPO_FILE <<EOF
[aliyun]
name=Aliyun YUM Repo
baseurl=https://mirrors.aliyun.com/centos/8-stream/BaseOS/x86_64/os/
gpgcheck=0
enabled=1
EOF
            cat > /etc/yum.repos.d/epel.repo <<EOF
[epel]
name=Extra Packages for Enterprise Linux 8 - $basearch
baseurl=https://mirrors.aliyun.com/epel/8/Everything/$basearch/
gpgcheck=0
enabled=1
EOF
            ;;
        9)
            cat > $REPO_FILE <<EOF
[aliyun]
name=Aliyun YUM Repo
baseurl=https://mirrors.aliyun.com/centos/9-stream/BaseOS/x86_64/os/
gpgcheck=0
enabled=1
EOF
            cat > /etc/yum.repos.d/epel.repo <<EOF
[epel]
name=Extra Packages for Enterprise Linux 9 - $basearch
baseurl=https://mirrors.aliyun.com/epel/9/Everything/$basearch/
gpgcheck=0
enabled=1
EOF
            ;;
        *)
            echo "不支持的系统版本: $OSVER"
            exit 1
            ;;
    esac
    echo "清理并生成YUM缓存..."
    if command -v yum &>/dev/null; then
        yum clean all && yum makecache
    elif command -v dnf &>/dev/null; then
        dnf clean all && dnf makecache
    fi
    echo "阿里云yum源配置完成！"
    exit 0
elif [ "$menu" = "3" ]; then
    # 单独配置EPEL源
    if [ -f /etc/redhat-release ]; then
        OSVER=$(grep -oE '[0-9]+' /etc/redhat-release | head -1)
    fi
    case $OSVER in
        6)
            cat > /etc/yum.repos.d/epel.repo <<EOF
[epel]
name=Extra Packages for Enterprise Linux 6 - $basearch
baseurl=https://mirrors.aliyun.com/epel/6/$basearch/
gpgcheck=0
enabled=1
EOF
            ;;
        7)
            cat > /etc/yum.repos.d/epel.repo <<EOF
[epel]
name=Extra Packages for Enterprise Linux 7 - $basearch
baseurl=https://mirrors.aliyun.com/epel/7/$basearch/
gpgcheck=0
enabled=1
EOF
            ;;
        8)
            cat > /etc/yum.repos.d/epel.repo <<EOF
[epel]
name=Extra Packages for Enterprise Linux 8 - $basearch
baseurl=https://mirrors.aliyun.com/epel/8/Everything/$basearch/
gpgcheck=0
enabled=1
EOF
            ;;
        9)
            cat > /etc/yum.repos.d/epel.repo <<EOF
[epel]
name=Extra Packages for Enterprise Linux 9 - $basearch
baseurl=https://mirrors.aliyun.com/epel/9/Everything/$basearch/
gpgcheck=0
enabled=1
EOF
            ;;
        *)
            echo "不支持的系统版本: $OSVER"
            exit 1
            ;;
    esac
    echo "EPEL源配置完成。"
    exit 0
fi

# 处理路径参数
if [ -n "$2" ]; then
    APPSTREAM_PATH="$1"
    BASEOS_PATH="$2"
else
    # 只传递一个参数时，自动拼接
    APPSTREAM_PATH="$1/AppStream"
    BASEOS_PATH="$1/BaseOS"
    # 仅在8/9自动创建目录
    if [[ "$OSVER" == "8" || "$OSVER" == "9" ]]; then
        if [ ! -d "$APPSTREAM_PATH" ]; then
            echo "未检测到 $APPSTREAM_PATH，自动创建..."
            mkdir -p "$APPSTREAM_PATH"
        fi
        if [ ! -d "$BASEOS_PATH" ]; then
            echo "未检测到 $BASEOS_PATH，自动创建..."
            mkdir -p "$BASEOS_PATH"
        fi
    fi
fi

# 检查路径
if [ ! -d "$APPSTREAM_PATH" ]; then
    echo "AppStream路径 $APPSTREAM_PATH 不存在！"
    exit 1
fi
if [ ! -d "$BASEOS_PATH" ]; then
    echo "BaseOS路径 $BASEOS_PATH 不存在！"
    exit 1
fi

# 检测主版本号
if [ -f /etc/redhat-release ]; then
    OSVER=$(grep -oE '[0-9]+' /etc/redhat-release | head -1)
else
    echo "无法检测RedHat版本！"
    exit 1
fi

BACKUP_DIR="/etc/yum.repos.d/backup_$(date +%Y%m%d_%H%M%S)"
mkdir -p "$BACKUP_DIR"
echo "备份原有repo文件到 $BACKUP_DIR ..."
mv /etc/yum.repos.d/*.repo "$BACKUP_DIR"/

case $OSVER in
    6|7)
        REPO_FILE="/etc/yum.repos.d/local.repo"
        echo "生成本地YUM源配置: $REPO_FILE"
        cat > $REPO_FILE <<EOF
[local]
name=Local YUM Repo
baseurl=file://$APPSTREAM_PATH
gpgcheck=0
enabled=1
EOF
        ;;
    8|9)
        REPO_FILE="/etc/yum.repos.d/itpux.repo"
        echo "生成本地YUM源配置: $REPO_FILE"
        cat > $REPO_FILE <<EOF
[EL${OSVER}-1]
name=Linux-${OSVER}-DVD1
baseurl=file://$APPSTREAM_PATH
gpgcheck=0
enabled=1

[EL${OSVER}-2]
name=Linux-${OSVER}-DVD2
baseurl=file://$BASEOS_PATH
gpgcheck=0
enabled=1
EOF
        ;;
    *)
        echo "不支持的RedHat主版本: $OSVER"
        exit 1
        ;;
esac

echo "清理并生成YUM缓存..."
if [ "$OSVER" -eq 6 ]; then
    yum clean all && yum makecache
else
    yum clean all && yum repolist || dnf clean all && dnf repolist
fi

echo "本地YUM源配置完成！"
